<template>
  <el-dialog v-model="dialogInfo.visible" :title="dialogInfo.title" :width="dialogInfo.width" align-center>
    <el-form ref="formRef" label-position="left" :model="formModel" :rules="formRules" label-width="120px">
      <el-row>
        <el-col :sm="24">
          <el-form-item label="专题名称" prop="topicName">
            <el-input clearable class="w100" v-model="formModel.topicName" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :sm="24">
          <el-form-item label="标签分类">
            <div>
              <el-tag v-for="tag in topicTypeList" :key="tag" class="mx-1" closable :disable-transitions="false" @close="handleClose(tag)">
                {{ tag }}
              </el-tag>

              <el-input v-if="inputVisible" ref="InputRef" v-model="inputValue" class="ml-1 w100px" placeholder="请输入" size="small" @keyup.enter="handleInputConfirm" @blur="handleInputConfirm" />
              <el-button v-else type="primary" size="small" @click="showInput">
                +
              </el-button>

            </div>

          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="info" plain @click="onCancel()">取消</el-button>
        <el-button type="primary" @click="onSubmit()">提交</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
/**
 * 管理员编辑组件
 */
import { ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import EsAjax from '@/utils/EsAjax';
import ApiUrls from '@/apis/index';
import { roleParams } from '@/apis/model/RoleModel';
import { getNewObj } from '@/utils/EsCommon';

// ======================================================
//                      变量声明
// ======================================================

// emit
const emit = defineEmits(['success']);
// 表单数据
const formAttrs = {
  id:null,
  topicName: '',
  topicType: '',
};
const topicTypeList = ref([])

const inputVisible = ref(false)
const inputValue = ref('')

const InputRef = ref(null);

const handleClose = (tag) => {
  topicTypeList.value.splice(topicTypeList.value.indexOf(tag), 1)
}

const showInput = () => {
  inputVisible.value = true
}

const handleInputConfirm = () => {
  if (inputValue.value) {
    topicTypeList.value.push(inputValue.value)
  }
  inputVisible.value = false
  inputValue.value = ''
}

const formModel = ref(Object.assign({}, formAttrs));
// 弹窗信息
const dialogInfo = ref({
  visible: false,
  title: '',
  width: '600px',
})
// 表单组件的引用实例
const formRef = ref(null);
const treeRef = ref(null);
// 表单校验规则
const formRules = ref({
  topicName: [
    { required: true, message: '请输入专题名称', trigger: 'blur' },
  ],

})

// ======================================================
//                      点击操作
// ======================================================

// 点击提交按钮
const onSubmit = () => {
  formRef.value.validate((valid, fields) => {
    if (!valid) {
      console.log('表单校验失败：', fields);
      return;
    }

    formModel.value.topicType = topicTypeList.value.join(',') || ''
    let url = formModel.value.id > 0 ? ApiUrls.topicChange : ApiUrls.topicAdd;
    EsAjax.post(url, formModel.value).then(res => {
      if (res.data.code !== 200) {
        ElMessage.warning(res.data.msg);
        return;
      }
      dialogInfo.value.visible = false;
      ElMessage.success('提交成功');
      emit('success');
    }).catch(err => {
      ElMessage.error('网络错误：' + err.message);
    })
  })
}

// 点击取消按钮
const onCancel = () => {
  formModel.value = Object.assign({}, formAttrs);
  formRef.value.resetFields();
  dialogInfo.value.visible = false;
}
// ======================================================
//                      处理事件
// ======================================================

// 加载数据
const show = async (id, title, item = null, width = '600px') => {
  dialogInfo.value.title = title;
  dialogInfo.value.width = width;
  dialogInfo.value.visible = true;
  formModel.value = Object.assign({}, formAttrs)
  topicTypeList.value = []
  // 加载详情数据
  if (id) {
    if (item.topicType) {
      item = getNewObj(item)
      topicTypeList.value = item.topicType.split(',')
    }
    formModel.value = Object.assign({ ...formAttrs }, getNewObj(item));
  }
}

defineExpose({
  show
})
</script>

<style scoped>
</style>
